package com.douya.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 说明：时间工具类
 *
 * @Author 胡晨烨
 * @Date 2020/02/01
 */
@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static Date now() {
        return new Date();
    }

    public static Date truncateToFiveMinute(Date Date) {
        return DateUtils.addMinutes(DateUtils.truncate(Date, Calendar.HOUR_OF_DAY),
                Integer.parseInt(DateFormatUtils.format(Date, "mm")) / 5 * 5);
    }

    public static String format(Date Date, String pattern) {
        return new SimpleDateFormat(pattern).format(Date);
    }

    public static Date parseDate(String str, String... parsePatterns) {
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(str, parsePatterns);
        } catch (ParseException e) {
            log.warn("Parse Date error:" + str, e);
            return null;
        }
    }

}
